const util = require("../utils/util")
const app = getApp()
Page({

  data: {
    plcaceHolder:'请输入内容'
  },
  onShow(){
    this.setData({
      openid: app.globalData.openid
    })
  },
  onLoad: function (options) {
    console.log(options.id)
    this.setData({
      recordId:options.id
    })

    this.getChatRecord()


  },

  getChatRecord(){

    var that = this;
    wx.cloud.database().collection('auto_reply_chatRecord').doc(that.data.recordId).get({
      success(res){
        console.log(res)
        that.setData({
          chatList: res.data.record
        })
      }
    })
  },


  getInputValue(event){

    console.log(event.detail.value)

    this.data.inputValue = event.detail.value
    
  },
  publishChat(){
    if(!this.data.inputValue){
      wx.showToast({
        icon:'error',
        title: '请输入内容',
      })
      return
    }
    var that = this;
    wx.cloud.database().collection('auto_reply_chatRecord').doc(that.data.recordId).get({
      success(res){
        console.log(res)


        var record = res.data.record
        var msg = {}

        msg.nickName = app.globalData.userInfo.nickName
        msg.avatarUrl = app.globalData.userInfo.avatarUrl
        msg.openid = app.globalData.openid
        msg.text = that.data.inputValue
        msg.time = util.formatTime(new Date())

        record.push(msg)

        wx.cloud.database().collection('auto_reply_chatRecord').doc(that.data.recordId).update({
          data: {
            record: record
          },
          success(res){
            console.log(res)
            wx.showToast({
              title: '发布成功！',
            })
            
            that.autoReply()

            

          }
        })


      }
    })

  
  },
  autoReply(){


    wx.cloud.database().collection('auto_reply')
    .where({
      question:wx.cloud.database().RegExp({
        regexp:this.data.inputValue,
        options:'i'
      })
    })
    .limit(4)
    .get()
    .then(result=>{

      console.log(result)
      let replyList = result.data
      if(replyList.length == 0){
        //刷新下
        this.getChatRecord()

        this.setData({
          inputValue :'',
          plcaceHolder:'请输入内容'
        })
        return
      }
      wx.cloud.database().collection('auto_reply_chatRecord').doc(this.data.recordId)
      .get()
      .then(res=>{
        console.log(res)
  
        var record = res.data.record
        var msg = {}


        msg.reply = replyList
        msg.time = util.formatTime(new Date())

        record.push(msg)

        wx.cloud.database().collection('auto_reply_chatRecord').doc(this.data.recordId).update({
          data: {
            record: record
          }
        })
        .then(res=>{
            console.log(res)
            wx.showToast({
              title: '发布成功！',
            })
            //刷新下
            this.getChatRecord()

            this.setData({
              inputValue :'',
              plcaceHolder:'请输入内容'
            })
        })

      })

    })


    

  },

  replyChoosedQuestion(event){

    console.log(event.currentTarget.dataset.id)


    wx.cloud.database().collection('auto_reply').doc(event.currentTarget.dataset.id)
    .get()
    .then(result=>{
      console.log(result)
      let autoReply = result.data

      wx.cloud.database().collection('auto_reply_chatRecord').doc(this.data.recordId)
      .get()
      .then(res=>{

        console.log(res)
        var record = res.data.record

        var msg1 = {}
        msg1.nickName = app.globalData.userInfo.nickName
        msg1.avatarUrl = app.globalData.userInfo.avatarUrl
        msg1.openid = app.globalData.openid
        msg1.text = event.currentTarget.dataset.question//当前点击的问题做为用户的输入
        msg1.time = util.formatTime(new Date())
        record.push(msg1)

        var msg2 = {}
        msg2.answer = autoReply.answer
        msg2.time = util.formatTime(new Date())
        record.push(msg2)

        wx.cloud.database().collection('auto_reply_chatRecord').doc(this.data.recordId).update({
          data: {
            record: record
          }
        })
        .then(res=>{
          console.log(res)
          //刷新下
          this.getChatRecord()
        })

      })
      




    })


  }
  
})